package com.sam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.Driver;
 
public class CopyOfMember {
	private String userId;
	private String userName;
	private String nickName;
	private String passWord;
	private String eMail;
	private String status;
	
	ArrayList<CopyOfMember> allMembers;
	
	private String userIdMsg;
	private String userNameMsg;
	private String passWordMsg;
	private String eMmailMsg;
	private String nickNameMsg;
	private String loginMsg;
	
	private boolean isLogin;
	private boolean isValid; 
	private boolean isSave;
	private boolean isDelete;
	private boolean isUser;
	
	private String referer;
						
	public CopyOfMember(){
		
	}
	
	protected CopyOfMember(String userId, String passWord) {
		super();
		this.userId = userId;
		this.passWord = passWord;
	}

	protected CopyOfMember(String userId, String userName, String nickName,
			String passWord, String eMail) {
		super();
		this.userId = userId;
		this.userName = userName;
		this.nickName = nickName;
		this.passWord = passWord;
		this.eMail = eMail;
	}
	
	protected CopyOfMember(String userId, String userName, String nickName,
			String passWord, String eMail, String status) {
		super();
		this.userId = userId;
		this.userName = userName;
		this.nickName = nickName;
		this.passWord = passWord;
		this.eMail = eMail;
		this.status = status;
	}

	public boolean checkField() {
		isValid = true;
		
		if(userId.length() < 4 || userId.length() > 20){
			isValid = false;
			userIdMsg = "使用者帳號長度不符合!!";
		}
		
		if(userName.length() < 4 || userName.length() > 20) {
			isValid = false;
			userNameMsg = "使用者名稱長度不符合!!";
		}
		
		if(nickName.length() < 4 || nickName.length() > 20) {
			isValid = false;
			nickNameMsg = "暱稱長度不符合!!";
		}
		
		if(passWord.length() < 4 || passWord.length() > 20) {
			isValid = false;
			passWordMsg = "密碼長度不符合!!";
		}
		
		if(eMail.indexOf("@") == -1) {
			isValid = false;
			eMmailMsg = "電子郵件格式不符合!!";
		}
		
		return isValid;
	}
	
	public boolean chkUsers(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		isUser = true;
		
		try {
			DriverManager.registerDriver(new Driver());
			conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8", "j102u17", "j102u17.989");
			String sql = "select * from users where userId=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()){
				isUser = false;
				loginMsg = "使用者帳號重覆!!";
			}else{
				loginMsg = "";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (conn!=null){
				try {
					pstmt.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return isUser;
	}
		
	public boolean saveMember() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		isSave = false;
		
		try {
			DriverManager.registerDriver(new Driver());
            conn = DriverManager.getConnection(
        		"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8", "j102u17", "j102u17.989");
            String sql = "insert into users(userId, userName, nickName, passWord, eMail, status) values (?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userId);
            pstmt.setString(2, userName);
            pstmt.setString(3, nickName);
            pstmt.setString(4, passWord);
            pstmt.setString(5, eMail);
            pstmt.setString(6, " ");
            int rowCount = pstmt.executeUpdate();
            if (rowCount == 1){
            	isSave = true;
            }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isSave;
	}
	
	public boolean delete(String userId) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		isDelete = false;
		
		try {
			DriverManager.registerDriver(new Driver());
            conn = DriverManager.getConnection(
        		"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8", "j102u17", "j102u17.989");
            String sql = "update users set status=? where userId =?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "Y");
            pstmt.setString(2, userId);
            int rowCount = pstmt.executeUpdate();
            if (rowCount == 1){
            	isDelete = true;
            }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isDelete;
	}
	
	public boolean login(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		isLogin = false;
		
		try {
			DriverManager.registerDriver(new Driver());
			conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8", "j102u17", "j102u17.989");
			String sql = "select * from users where userId=? and passWord=? and status=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			pstmt.setString(2, passWord);
			pstmt.setString(3, " ");
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()){
				isLogin = true;
				nickName = rs.getString("nickName");
			}else{
				loginMsg = "登入失敗!!";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (conn!=null){
				try {
					pstmt.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return isLogin;
	}
	
	public ArrayList<CopyOfMember> getAllMembers(){
		if (allMembers==null)
			allMembers = new ArrayList<CopyOfMember>();
		allMembers.clear();
		try {
			DriverManager.registerDriver(new Driver());
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102u17?useUnicode=true&characterEncoding=UTF-8",
					"j102u17", "j102u17.989");
			String sql = "select userId, passWord, nickName, userName, eMail, status from users where status=? order by userId";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, " ");
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				userId = rs.getString("userId");
				passWord = rs.getString("passWord");
				nickName = rs.getString("nickName");
				userName = rs.getString("userName");
				eMail = rs.getString("eMail");
				status = rs.getString("status");
				CopyOfMember m = new CopyOfMember(userId, passWord, nickName, userName, eMail, status );
				allMembers.add(m);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return allMembers;
	}
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserNameMsg() {
		return userNameMsg;
	}

	public void setUserNameMsg(String userNameMsg) {
		this.userNameMsg = userNameMsg;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String geteMail() {
		return eMail;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUserIdMsg() {
		return userIdMsg;
	}

	public void setUserIdMsg(String userIdMsg) {
		this.userIdMsg = userIdMsg;
	}

	public String getPassWordMsg() {
		return passWordMsg;
	}

	public void setPassWordMsg(String passWordMsg) {
		this.passWordMsg = passWordMsg;
	}

	public String geteMmailMsg() {
		return eMmailMsg;
	}

	public void seteMmailMsg(String eMmailMsg) {
		this.eMmailMsg = eMmailMsg;
	}

	public String getNickNameMsg() {
		return nickNameMsg;
	}

	public void setNickNameMsg(String nickNameMsg) {
		this.nickNameMsg = nickNameMsg;
	}

	public String getLoginMsg() {
		return loginMsg;
	}

	public void setLoginMsg(String loginMsg) {
		this.loginMsg = loginMsg;
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public boolean isValid() {
		return isValid;
	}

	public void setValid(boolean isValid) {
		this.isValid = isValid;
	}

	public boolean isSave() {
		return isSave;
	}

	public void setSave(boolean isSave) {
		this.isSave = isSave;
	}

	public boolean isUser() {
		return isUser;
	}

	public void setUser(boolean isUser) {
		this.isUser = isUser;
	}

	public void setAllMembers(ArrayList<CopyOfMember> allMembers) {
		this.allMembers = allMembers;
	}
	
	public String getReferer() {
		if (referer!=null && referer.substring(referer.lastIndexOf("/")).equals("/index.jsp")){
			referer = null;
		}
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
		System.out.println("setReferer:"+referer);
	}
}
